<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\BannerModel;
use think\facade\Db;
use think\facade\View;

class Banner extends AdminBase
{

    /**
     * 轮播图Banner列表
     */
    public function index()
    {
        $param = $this->request->param();
        $type = $param['type'];
        $model = new BannerModel();
        $where = 1;
        if( $type!='' ) {
            $where .= " and type = '$type'";
        }
        $table_name = $model->getTable();
        $list = Db::query("select count(1) as count,`type`,group_concat(id order by sort asc) as ids,group_concat(image order by sort asc) as images,group_concat(title order by sort asc) as titles from $table_name where $where group by `type`");
        $list = array_map(function($item) {
            $item['images'] = explode(',',$item['images']);
            $item['ids'] = explode(',',$item['ids']);
            $item['titles'] = explode(',',$item['titles']);
            return $item;
        },$list);
        //type种类
        $this->assignTypes();

        return view('',compact('list','type'));
    }

    /**
     * 添加轮播图Banner
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $model = new BannerModel();
            $param = $this->request->param();

            //当前类型图片数量
            $type = $param['type'];
            $last_sort = BannerModel::where('type', $type)->order('sort desc')->value('sort');
            $param['sort'] = intval($last_sort) + 1;

            $result = BannerModel::create($param);
            if( $result ) {
                xn_add_admin_log('添加轮播图Banner');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        //type种类
        $this->assignTypes();
        return view('form',[]);
    }

    /**
     * 编辑轮播图Banner
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = BannerModel::update($param);
            if( $result ) {
                xn_add_admin_log('编辑轮播图Banner');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = BannerModel::where('id',$id)->find();
        //type种类
        $this->assignTypes();
        return view('form',compact('data'));
    }

    /**
     * 删除轮播图Banner
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $type = $this->request->param('type');
        if ($id) {
            BannerModel::destroy($id);
        } else if ($type != '') {
            BannerModel::where('type',$type)->delete();
        } else {
            $this->error('参数错误');
        }
        xn_add_admin_log('删除轮播图Banner');
        $this->success('删除成功');
    }

    //排序
    public function sort() {
        $id = $this->request->get('id/d');
        $to = $this->request->get('to');
        $model = new BannerModel();
        if ( $to == 'left' ) {
            $data1 = $model->where('id',$id)->find();
            $data2 = $model->where('type',$data1['type'])->where('sort','<',$data1['sort'])->order('sort desc')->find();
            if ( $data2) {
                $model->where('id',$data1['id'])->update(['sort'=>$data2['sort']]);
                $model->where('id',$data2['id'])->update(['sort'=>$data1['sort']]);
                xn_add_admin_log('更新轮播图排序');
            }
            $this->success('排序成功');
        } elseif ( $to == 'right' ) {
            $data1 = $model->where('id',$id)->find();
            $data2 = $model->where('type',$data1['type'])->where('sort','>',$data1['sort'])->order('sort asc')->find();
            if ( $data2) {
                $model->where('id',$data1['id'])->update(['sort'=>$data2['sort']]);
                $model->where('id',$data2['id'])->update(['sort'=>$data1['sort']]);
                xn_add_admin_log('更新轮播图排序');
            }
            $this->success('排序成功');
        }
    }

    private function assignTypes()
    {
        $types = BannerModel::field('type as title')->group('type')->select();
        View::assign('types',json_encode($types));
    }
}